<?php
if ( !defined( "IN_OP" ) )
{
    exit( "You can't access this file directly..." );
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval( getvar( "gametype" ) );
$chkGametype = chkGametype( $gametype );
$chk = new CheckMode_3d( $curruser['acl'], 1, $gametype );
$chk->checkShow3dPage();
$show3D3S = $chk->show3D3S;
if ( $show3D3S == 1 || $show3D3S == 2  || $show3D3S == 3)
{
    $type = true;
}
else
{
    $type = false;
}
unset( $chk );
$tbl = $g_gametype_3d[$gametype]['en'];
$tblName = $g_gametype_3d[$gametype]['ch'];
include_once( "class/function_3d.php" );
$ui = new UI_3D( true );
$tpl = new Template2();
$conf = new Config();
$paperShowOpen = getPaperMinDate( 0 - 1 );
$postTime = $conf->get( "paper_post_bet" );
if ( date( "Hi" ) <= $postTime )
{
    $todayInt = strtotime( "-1 day" );
    $today = date( "Y-m-d", $todayInt );
}
else
{
    $todayInt = time();
    $today = date( "Y-m-d", $todayInt );
}
$dateTempM = date( "m", $todayInt );
$dateTempD = date( "d", $todayInt );
$dateTempW = date( "w", $todayInt );
$dateTempY = date( "Y", $todayInt );
if ( $dateTempW == 0 )
{
    $secThisWeek1 = mktime( 0, 0, 0, $dateTempM, $dateTempD - 6, $dateTempY );
}
else
{
    $secThisWeek1 = mktime( 0, 0, 0, $dateTempM, $dateTempD - $dateTempW + 1, $dateTempY );
}
$calThisWeek1 = date( "Y-m-d", $secThisWeek1 );
$calLastWeek1 = date( "Y-m-d", $secThisWeek1 - 604800 );
$calLastWeek2 = date( "Y-m-d", $secThisWeek1 - 1 );
$minday = getPaperMinDate( 0 - 1 );
$begin = getvar( "begin" ) == "" ? $calThisWeek1 : getvar( "begin" );
$end = getvar( "end" ) == "" ? $today : getvar( "end" );
$check = getvar( "begin" ) == "" ? "2" : getvar( "check" );
$title = "<span style=\"padding:9px 0px 0px 8px;\" class=\"head1\">{$tblName}帐户历史&nbsp;│【日期选择】：</span>";
$title .= "<span style=\"padding:3px 0px 0px 5px;\" class=\"head1\">";
$title .= "<input type=\"button\" value=\"上周\" class=\"".( $check == 1 ? "btn btn-default active" : "btn btn-default" )."\" onclick=\"javascript:date(0);\">&nbsp;";
$title .= "<input type=\"button\" value=\"本周\" class=\"".( $check == 2 ? "btn btn-default active" : "btn btn-default" )."\" onclick=\"javascript:date(1);\">&nbsp;";
$title .= "<input type=\"button\" value=\"全部\" class=\"".( $check == 3 ? "btn btn-default active" : "btn btn-default" )."\" onclick=\"javascript:date(2);\"></span>";
$tpl->set( array( "today" => $today, "minday" => $minday, "calThisWeek1" => $calThisWeek1, "calLastWeek1" => $calLastWeek1, "calLastWeek2" => $calLastWeek2, "begin" => $begin, "end" => $end ) );
if ( $today == $end )
{
    $oneday = 86400;
}
else
{
    $oneday = 0;
}
$today = max( $end, $minday );
$minday = max( $begin, $minday );
$lastday = date( "Y-m-d", strtotime( $today ) - $oneday );
$showDate = getvar( "showDate" );
$showDate = $showDate == "" ? $today : $showDate;
if ( $conf->get( "calcbet_{$tbl}" ) == 1 )
{
    $ui->showErrorPage( "{$tblName}颁奖过帐中，请稍後再使用本功能！", true );
    exit();
}
else if ( $curruser['enabled'] == 0 )
{
    $ui->showErrorPage( "帐号已被暂停使用" );
    exit();
}
require_once( "class_3d/MyLotto_3d.php" );
require_once( "class_3d/Bet_3d.php" );
$MyLotto_3d = new MyLotto_3d( $gametype );
$Bet_3d = new Bet_3d( $gametype );
$tmp = "";
$sum_amount = 0;
$sum_result = 0;
$sum_back1 = 0;
if ( $type )
{
    $openRow = "openbet";
    $sql1 = "SELECT B.* ,sum(A.amount) as amount,sum(A.b1) as b1,sum(A.a1) as a1 FROM {$Bet_3d->table_name} A Left Join {$MyLotto_3d->table_name} B on A.lotto_id = B.id where A.user_id = ".$curruser['id']." AND B.open= '{$today}' GROUP BY (B.open)";
    $sql1 .= " UNION ALL SELECT B.* ,sum(A.amount) as amount,sum(A.b1) as b1,sum(A.a1) as a1 FROM x{$Bet_3d->table_name} A Left Join {$MyLotto_3d->table_name} B on A.lotto_id = B.id where A.user_id = ".$curruser['id']." AND B.open BETWEEN '{$minday}' AND '{$lastday}' GROUP BY (B.open) ORDER BY open DESC";
	$rs = getdb( $sql1 );
}
else
{
    $page = 0;
    $openRow = "open";
    $rs = $MyLotto_3d->getDataBeforeOpen( $today, $minday, $show3D3S, $page );
}

if ( $end < getPaperMinDate( 0 - 1 ) )
{
    $list = "<tr ><td align=\"left\" colspan=\"5\">查询日期小於可看最小报表日</td></tr>";
}
else if ( $rs->eof )
{
    $list = "<tr ><td colspan=\"5\">查无".$tblName."资料</td></tr>";
}
else
{
    $list = "";
}
$tblChk = $Bet_3d->table_name;
while ( !$rs->eof )
{
    $lotto_id = $rs->f['id'];
    $open = $rs->f['open'];
    $round = $rs->f['round'];
    $openChk = $rs->f['open'];
    if ( $openChk == $today )
    {
        $Bet_3d->table_name = $tblChk;
    }
    else
    {
        $Bet_3d->table_name = "x".$tblChk;
    }
    if ( $type )
    {
        $totalResult = $rs;
    }
    else
    {
        $sql = "SELECT sum(amount) as amount,sum(b1) as b1,sum(a1) as a1 FROM ".$Bet_3d->table_name;
        $sql .= " WHERE user_id = ".$curruser['id'];
        $sql .= " AND lotto_id = ".$lotto_id;
        $totalResult = getdb( $sql );
    }
    if ( $end < $rs->f['open'] )
    {
        $list = "<tr class=\"row1\"><td align=\"center\" colspan=\"5\">查询日期小於可看最小报表日</td></tr>";
    }
    else
    {
        $list .= "<tr class=\"td2\">";
        if ( $type && $totalResult->f['calcbet'] == 1 )
        {
            $list .= "<td colspan=\"5\" class=\"td_cen_b\">".$open." 期数:".$round."颁奖过帐中</td>";
            $rs->movenext();
            continue;
        }
        if ( 0 < $totalResult->f['amount'] )
        {
            if ( $show3D3S == 2 )
            {
                $showUrl = "?op=member_5h&fp=history_round&lotto_id=".$lotto_id."&gametype=".$gametype."&showDate=".$open;
            }
            else if ( $type )
            {
                $showUrl = "?op=member_3d&fp=history_round&lotto_id=".$lotto_id."&gametype=".$gametype."&showDate=".$open;
            }
            else
            {
                $showUrl = "?op=member_3d&fp=view&lotto_id=".$lotto_id."&gametype=".$gametype;
            }
            $list .= "<td nowrap><a href=\"".$showUrl."\"><font color=\"#cc0000\"><b>".$open."</b></font></a></td>";
            $list .= $type ? "" : "<td nowrap align=\"left\"><a href=\"".$showUrl."\"><font color=\"#cc0000\"><b>".$round."</b></font></a></td>";
        }
        else
        {
            $list .= "<td nowrap><font color=\"#cc0000\">".$open."</font></td>";
            $list .= $type ? "" : "<td nowrap align=\"left\"><font color=\"#cc0000\">".$round."</font></td>";
        }
        $list .= "<td align=\"right\">".$totalResult->f['amount']."</td>";
        $list .= "<td align=\"right\">".round( $totalResult->f['a1'], 2 )."</td>";
        $list .= "<td align=\"right\">".say_numbercolor( round( $totalResult->f['b1'], 2 ) )."</td>";
        $list .= "</tr>";
        $sum_amount += $totalResult->f['amount'];
        $sum_result += $totalResult->f['b1'];
        $sum_back1 += $totalResult->f['a1'];
    }
    $rs->movenext();
}
$jslink = "<script> ";
$jslink .= "$(function(){ ";
$jslink .= "  $('select#showGameType').change(function(){ ";
$jslink .= "    _showGameType = $(this).val(); ";
$jslink .= "    _showDate = $('#slcShowDate').val(); ";
$jslink .= "    if(_showGameType == -1){window.top.mainFrame.location.replace('?op=member_3d&fp=history_all');} ";
if ( $g_init_mode_6h )
{
    $jslink .= "\r\n    else if(_showGameType == 0){\r\n      window.top.leftFrame.location.replace('?op=member&fp=account');\r\n      window.top.mainFrame.location.replace('?op=member&fp=history');\r\n    }  \r\n";
}
$jslink .= "\r\n    else if(_showGameType >= 20){\r\n      window.top.leftFrame.location.replace('?op=member_5h&fp=account&gametype='+_showGameType);\r\n      window.top.mainFrame.location.replace('?op=member_5h&fp=history&gametype='+_showGameType);\r\n    }\r\n    else{\r\n      window.top.leftFrame.location.replace('?op=member_3d&fp=account&gametype='+_showGameType);\r\n      window.top.mainFrame.location.replace('?op=member_3d&fp=history&gametype='+_showGameType);\r\n    }\r\n  });\r\n";
$slcList = "";
$slcList = "<div class=\"col-sm-3\"><select name=\"showGameType\" id=\"showGameType\" class=\"form-control\">";
$slcList .= "<option class=\"paperall\" value=\"-1\">全部</option>";
if ( !$g_init_mode_6h )
{
    unset( $g_gametype_3d[0] );
}
foreach ( $g_gametype_3d as $key => $val )
{
    $en = $val['en'];
    if ( $key == $gametype )
    {
        $slced = "selected=\"selected\"";
    }
    else
    {
        $slced = "";
    }
    if ( $g_menu_enabled[$key] )
    {
        $slcList .= "<option class=\"paper{$en}\" value=\"{$key}\" {$slced} >";
        /*if ( $key == 20 )
        {
            $slcList .= "４+１码◎";
        }
        else if ( 20 < $key )
        {
            $slcList .= "八码◎";
        }
        else if ( $key && $g_5number_3d )
        {
            $slcList .= getShow35( $key ) ? "五码◎" : "三码◎";
        }*/
        $slcList .= $val['ch']."</option>";
    }
}
$slcList .= "</select></div>";
$jslink .= "  \r\n});\r\n</script>\n";
$ui->set( "jslink", $jslink );
//$tpl->load( "main", "history.htm" );
$tpl->load( array("menu" => ($gametype==24?'pk':'ssc')."_menu.htm","main" => "history.htm") );
$tpl->set("gametype",$gametype);
$tpl->show( "menu" );
$tpl->set( array( "list" => $list, "sum_amount" => $sum_amount, "sum_result" => say_numbercolor( round( $sum_result, 2 ) ), "sum_back1" => round( $sum_back1, 2 ), "sum_all" => round( $sum_result + $sum_back1, 2 ), "gametypeStr" => $tblName, "slcList" => $slcList, "gametype" => $gametype, "title" => $title, "htmltag_date" => "&nbsp;【{$begin}&nbsp;~&nbsp;{$end}】", "showDate" => $showDate, "round_td" => $type ? "" : "<td>期数</td>", "colspan" => $type ? "1" : "2" ) );
$ui->header();
$ui->showNews3D( $curruser['acl'] ,1);
$tpl->show( "main" );
$ui->footer();
?>